//
//  UserRequest.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/3.
//

#import "UserRequest.h"
#import "BaseBodyModel.h"

@implementation UserRequest

//登陆接口
+ (void)loginWithAccount:(NSString *)account password:(NSString *)password result:(void(^)(UserModel *user))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_Login];
    NSDictionary *parm = @{
                           @"account":account,
                           @"password":password
                           };
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 0) {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                } else {
                    UserModel *user = [UserModel mj_objectWithKeyValues:dic[@"userinfo"]];
                    resultBlock(user);
                }
            }
            
        }
    }];
}

//登陆接口
+ (void)thirdLoginWithAccount:(NSString *)account password:(NSString *)password result:(void(^)(UserModel *user))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_Login];
    NSDictionary *parm = @{
                           @"account":account,
                           @"password":password
                           };
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 0) {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                } else {
                    UserModel *user = [UserModel mj_objectWithKeyValues:dic[@"userinfo"]];
                    resultBlock(user);
                }
            }
            
        }
    }];
}

//获取邮箱验证码
+ (void)getEmali:(NSString *)event email:(NSString *)email result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_EmailSend];
    NSDictionary *parm = @{
                           @"email":email,
                           @"event":event
                           };
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 0) {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(NO);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(YES);
                }
            }
            
        }
    }];
}

//注册接口
+ (void)registerWithEmail:(NSString *)email code:(NSString *)code password:(NSString *)password result:(void(^)(UserModel *user))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_Register];
    NSDictionary *parm = @{
                           @"email":email,
                           @"code":code,
                           @"password":password
                           };
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 0) {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                } else {
                    UserModel *user = [UserModel mj_objectWithKeyValues:dic[@"userinfo"]];
                    resultBlock(user);
                }
                
            }
        }
    }];
}

//完善接口
+ (void)completeUserFileWithName:(NSString *)name age:(NSUInteger)age weight:(NSString *)weight gender:(NSUInteger)gender result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_CompleFile];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (weight.length > 0) {
        NSDictionary *parm = @{
                               @"name":name,
                               @"age":@(age),
                               @"gender":@(gender),
                               @"weight":weight,
                               @"perfect": @"1"
                               };
        [dic setDictionary:parm];
    } else {
        NSDictionary *parm = @{
                               @"name":name,
                               @"age":@(age),
                               @"gender":@(gender),
                               @"perfect": @"1"
                               };
        [dic setDictionary:parm];
    }
   
    [self basePOST:url cache:NO autoHideLoading:NO parms:dic result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
                if (model.code == 0) {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(NO);
                } else {
                    resultBlock(YES);
                }
                
            }
        }
    }];
}

//找回密码接口
+ (void)resetPasswordWithEmail:(NSString *)email captcha:(NSString *)captcha password:(NSString *)password result:(void(^)(NSInteger code))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_ResetPassword];
    NSDictionary *parm = @{
                           @"email":email,
                           @"captcha":captcha,
                           @"newpassword":password
                           };
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
            resultBlock(model.code);
        }
    }];
}

+ (void)thirdLoginWithPlatform:(NSString *)platform code:(NSString *)code result:(void(^)(UserModel *user))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_ThirdLogin];
    NSDictionary *parm = @{
                           @"platform":platform,
                           @"code":code
                           };
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 0) {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                } else {
                    UserModel *user = [UserModel mj_objectWithKeyValues:dic[@"userinfo"]];
                    resultBlock(user);
                }
            }
            
        }
    }];
}

+ (void)logoutResult:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_Logout];

    [self basePOST:url cache:NO autoHideLoading:NO parms:nil result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
            resultBlock(model.code == 0);
        }
    }];
}

+ (void)getNoticeNumResult:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl, Cyc_API_MessageNum];
    [self baseGET:url cache:NO autoHideLoading:NO parms:nil request:^(id  _Nullable resultObject, NSError * _Nullable error) {
        NSLog(@"**********===================%@",resultObject);
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 1) {
                    if ([dic[@"is_read"] intValue] == 0) {
                        resultBlock(NO);
                    } else {
                        resultBlock(YES);
                    }
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(NO);
                }
            }
            
        }
    }];
}
@end
